package com.qhsolution.bdict;

import java.io.IOException;

import com.google.ads.Ad;
import com.google.ads.AdListener;
import com.google.ads.AdRequest;
import com.google.ads.AdRequest.ErrorCode;
import com.google.ads.InterstitialAd;
import com.google.analytics.tracking.android.EasyTracker;
import com.qhsolution.adapter.AdapterTab;
import com.qhsolution.bdict.R;
import com.qhsolution.dbhelper.DataBaseHelper;

import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.app.ActionBar.Tab;
import android.app.ActionBar;
import android.app.ActionBar.TabListener;
import android.app.FragmentTransaction;
import android.content.Context;
import android.content.Intent;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.view.Menu;
import android.view.MenuItem;

public class MainActivity extends FragmentActivity implements TabListener, AdListener  {
	
	private InterstitialAd interstitial;
	
	private ViewPager viewPager;
	private AdapterTab adapterTab;
	private ActionBar actionBar;
	
	public final static String WORD_ID = "com.qhsolution.bdict.WORD_ID";
	private static final int RESULT_SETTINGS = 1;
	
	public static String LANGUAGE_FOR_SEACH_ENG = "ENG"; //Default ENG for search
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		interstitial = new InterstitialAd(this, getResources().getString(R.string.AD_POPUP_UNIT_ID));

	    // Create ad request
	    AdRequest adRequest = new AdRequest();

	    // Begin loading your interstitial
	    interstitial.loadAd(adRequest);

	    // Set Ad Listener to use the callbacks below
	    interstitial.setAdListener(this);

		
		viewPager=(ViewPager) findViewById(R.id.pager);
		
		actionBar=getActionBar();
		//actionBar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#7dcf77")));
		
		adapterTab=new AdapterTab(getSupportFragmentManager());
		
		viewPager.setAdapter(adapterTab);
		
		
		actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);//thiết lập Tab cho actionBar
		
		//3 Tabs: Lookup, Bookmark and About
		Tab a=actionBar.newTab();
		a.setText(getResources().getString(R.string.tab_1));
		a.setIcon(R.drawable.ic_action_search);
		a.setTabListener(this);
		
		Tab b= actionBar.newTab();
		b.setIcon(R.drawable.ic_action_favorite);
		b.setText(getResources().getString(R.string.tab_2));
		b.setTabListener(this);
		

		Tab c= actionBar.newTab();
		c.setIcon(R.drawable.ic_action_currency);
		c.setText(getResources().getString(R.string.tab_3));
		c.setTabListener(this);
		
		actionBar.addTab(a);
		actionBar.addTab(b);
		actionBar.addTab(c);
		
		viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
			
			@Override
			public void onPageSelected(int arg0) {
				// TODO Auto-generated method stub
				actionBar.setSelectedNavigationItem(arg0);
			}
			
			@Override
			public void onPageScrolled(int arg0, float arg1, int arg2) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void onPageScrollStateChanged(int arg0) {
				// TODO Auto-generated method stub
				
			}
		});
		
		DataBaseHelper db = new DataBaseHelper(getApplicationContext());
		try {
			//Create Database
			db.createDataBase();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
	    // Handle presses on the action bar items
	    switch (item.getItemId()) {
		    case R.id.action_settings:
	            Intent i = new Intent(this, SettingActivity.class);
	            startActivityForResult(i, RESULT_SETTINGS);
	            return true;	
		    case R.id.action_vote:
		    	shareAppIntent();
		    	return true;
	        default:
	            return super.onOptionsItemSelected(item);
	    }
	}
	
	@Override
	public void onTabReselected(Tab arg0, FragmentTransaction arg1) {
		// TODO Auto-generated method stub
	}

	@Override
	public void onTabSelected(Tab arg0, FragmentTransaction arg1) {
		// TODO Auto-generated method stub
				viewPager.setCurrentItem(arg0.getPosition());
	}

	@Override
	public void onTabUnselected(Tab arg0, FragmentTransaction arg1) {
		// TODO Auto-generated method stub
		
	}
	

	//Check is Internet Connected
	private boolean isNetworkConnected() {
		ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo ni = cm.getActiveNetworkInfo();
		if (ni == null) {
			// There are no active networks.
			return false;
		} else
			return true;
	}
	
	@Override
	public void onStart() {
	    super.onStart();
	    EasyTracker.getInstance(this).activityStart(this);  // Add this method.
	 }

	 @Override
	 public void onStop() {
	    super.onStop();
	    EasyTracker.getInstance(this).activityStop(this);  // Add this method.
	 }
	 
	 private void shareAppIntent()
	 {
	 	String SHARE_APP_DATA = getString(R.string.str_vote_app) + getPackageName();
	 	Intent sendIntent = new Intent();
	 	sendIntent.setAction(Intent.ACTION_SEND);
	 	sendIntent.putExtra(Intent.EXTRA_TEXT, SHARE_APP_DATA);
	 	sendIntent.setType("text/plain");
	 	startActivity(sendIntent);
	 }

	@Override
	public void onDismissScreen(Ad arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onFailedToReceiveAd(Ad arg0, ErrorCode arg1) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onLeaveApplication(Ad arg0) {
		// TODO Auto-generated method stub
		if (interstitial != null && interstitial.isReady())
			interstitial.show();
	}

	@Override
	public void onPresentScreen(Ad arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onReceiveAd(Ad arg0) {
		// TODO Auto-generated method stub
	}
	
	@Override
    public void onBackPressed() {
	 	if (interstitial != null && interstitial.isReady())
			interstitial.show();
    	finish();
    }
	
	  
}
